#!/bin/bash
# echo the call so we can see what's going on
echo "$0 $1 $2 $3 $4 $5 $6"

# Check syntax
if [ "$1" = "" ]; then
	echo "$0 called without desired section - assuming Z section!"
fi

# generate the Fourier difference map (this works currently only if no other map was generated)
echo "k f" > temp.txt
echo "1" >> temp.txt		# Fourier maps of other phases are not supported, so phase 1 should be the phase of interest!
echo "DELF" >> temp.txt
if [ "$1" = "" ]; then
  echo "z" >> temp.txt
else 
  echo $1 >> temp.txt
fi  
# no individual step size for the different directions
echo "n">> temp.txt
# step size in A
echo "0.2">> temp.txt
# limits along a, b, and c (do full cell, cut in VESTA)
echo "0 1">> temp.txt
echo "0 1">> temp.txt
echo "0 1">> temp.txt
# get number of histograms so we can include them all
grep NHST `cat GSAS_EXP`.EXP | tail -c67 | head -c65 | sed "s/ //g" > nhist.txt
NHIST=`cat nhist.txt`
echo "We have <$NHIST> histograms in this refinement."
COUNTER=0
while [ $COUNTER -lt $NHIST ]; do
	let COUNTER=COUNTER+1 
	echo $COUNTER >> temp.txt
done
echo "0" >> temp.txt
# add all other maps
echo "a FOBS" >> temp.txt
echo "a FCLC" >> temp.txt
echo "a NFDF" >> temp.txt
echo "2" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt

expedt `cat GSAS_EXP` < temp.txt > out.txt

fourier `cat GSAS_EXP`
# make input for forplot to convert for VESTA
echo "z e" > temp.txt
echo "DELF" >> temp.txt
echo "e" >> temp.txt
echo "f FOBS" >> temp.txt
echo "e" >> temp.txt
echo "f FCLC" >> temp.txt
echo "e" >> temp.txt
echo "f 2FDF" >> temp.txt
echo "e" >> temp.txt
echo "q" >> temp.txt
echo "Creating .grd files for VESTA..."
forplot `cat GSAS_EXP` < temp.txt > out.txt
echo "Done."

# Output stuff to the Latex document.
echo "Generated Fourier maps for phase 1 and converted them to files readable by VESTA.\begin{itemize}"  >> `cat GSAS_EXP`.tex
echo " \item\verb=`cat GSAS_EXP`_FOBS.grd= (the observed Fourier map), "  >> `cat GSAS_EXP`.tex
echo " \item\verb=`cat GSAS_EXP`_FCLC.grd= (the calculated Fourier map), "   >> `cat GSAS_EXP`.tex
echo " \item\verb=`cat GSAS_EXP`_DELF.grd= (the difference Fourier map \$F_o-F_c\$ ), and "  >> `cat GSAS_EXP`.tex
echo " \item\verb=`cat GSAS_EXP`\_2FDF.grd= (the Fourier map \$2F_o-F_c\$ ).\end{itemize} " >> `cat GSAS_EXP`.tex
echo "Koichi Momma's software VESTA can read those files, so you can view the " >> `cat GSAS_EXP`.tex
echo "Fourier maps in 3D and compare them with the current crystal structure. You may download VESTA from"  >> `cat GSAS_EXP`.tex
echo " "  >> `cat GSAS_EXP`.tex
echo "\begin{center}\verb=http://www.geocities.jp/kmo_mma/crystal/en/vesta.html=  \end{center}"  >> `cat GSAS_EXP`.tex
echo "Open the grd file in VESTA."  >> `cat GSAS_EXP`.tex
echo "To modify the contour levels in VESTA, go to Objects -- Properties -– Isosurfaces, select an isosurface in the list (roughly center of the dialog box, next to new, delete and clear buttons) and modify the level, color etc. above the list."  >> `cat GSAS_EXP`.tex
echo "In order to also plot the atoms, go to Edit Data -– Structure Parameters and click on import (bottom right) and read the EXP file \verb=`cat GSAS_EXP`.EXP= with the current crystal structure." >> `cat GSAS_EXP`.tex
echo "If you save a VESTA file with a .grd and your current imported EXP file, you may just re-open the VESTA file to obtain updated structure plots.\newline" >> `cat GSAS_EXP`.tex
